<?php
//后台登陆控制
include_once('admin_global.php');
$db->Get_user_shell_check($uid, $shell);   //检测用户是否登录
$db->Get_user_type_check($uid, $shell);    //检测用户权限
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>添加文章</title>
    <link rel="stylesheet" href="css/right.css" type="text/css" />
    <script type="text/javascript" src="../kindeditor/kindeditor.js"></script>
    <script type="text/javascript" src="../kindeditor/lang/zh_CN.js"></script>
    <script>
        KindEditor.ready(function(K) {
            var editor = K.create('textarea[name="content"]', {
                cssPath : '../kindeditor/plugins/code/prettify.css',
                uploadJson : '../kindeditor/php/upload_json.php',
                fileManagerJson : '../kindeditor/php/file_manager_json.php',
                allowFileManager : true,
                afterBlur: function(){this.sync();},              //编辑器失去焦点后同步KindEditor的值到textarea文本框，这一行很重要。
                afterCreate : function() {
                    var self = this;
                    K.ctrl(document, 13, function() {
                        self.sync();
                        K('form[name=example]')[0].submit();
                    });
                    K.ctrl(self.edit.doc, 13, function() {
                        self.sync();
                        K('form[name=example]')[0].submit();
                    });
                }
            });
            prettyPrint();
        });
    </script>
    <script>
        window.onload=function(){
            var myform=document.getElementById("form1");
            myform.onsubmit=check_addPageNews;
            function check_addPageNews(){
                if(myform.newsTitle.value==""){
                    alert("请输入文章标题！");
                    myform.newsTitle.focus();
                    return false;
                }
                if(myform.pid.value==0){
                    alert("请选择文章所属栏目");
                    return false;
                }
                if(myform.newsAuthor.value==""){
                    alert("请输入文章作者！");
                    myform.newsAuthor.focus();
                    return false;
                }
                if(myform.content.value==""){
                    alert('请输入文章内容！');
                    return false;
                }
            }
        }
    </script>
</head>
<?php
//查询出已有栏目类别，将其存入数组
$sql = "SELECT * FROM category";
$query = $db->query($sql);
while ($row = $db->fetch_array($query)) {
    $data[] = $row;
}
//获取该栏目ID,输出树形类
$cateID = $_GET['id'];                //通过get方式取得栏目ID
$tree = new Tree($data);                  //树形类实例化
$gettree = $tree->get_tree(0, "<option value=\$id \$selected>\$spacer\$cateName</option>", $cateID);             //取得带有当前栏目位置的树形结构
//查看该栏目下是否有文章，有就读取出来
$sql = "SELECT * FROM news WHERE newsCID=$cateID";
$query = $db->query($sql);
$num =$db->db_num_rows($query);
if ($num == 0) {
    $rs = array('newsTitle' => '', 'newsAuthor' => '', 'newsSource' => '', 'newsKeyWords' => '', 'newsContent' => '');
} else {
    $rs = $db->fetch_array($query);
}

//将文章内容写入数据库或修改数据库内容
if (isset($_POST['addPageNews'])) {
    //获取POST数组变量
    $newsTitle = trim($_POST['newsTitle']);
    $newsKeyWords = $_POST['newsKeyWords'];
    $newsAuthor = $_POST['newsAuthor'];
    $newsSource = $_POST['newsSource'];
    $newsSendTime = time();
    $newsUpdateTime=$newsSendTime;
    //判断是否有图片
    $newsContentTMP = $_POST['content'];
    $patten = '/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i';
    if (preg_match($patten, $newsContentTMP, $matche)) {
        $newsImagePath = $matche[1];
    } else {
        $newsImagePath = "";
    }
    if (!get_magic_quotes_gpc()) {
        $newsContent = addslashes($_POST['content']);
    } else {
        $newsContent = $_POST['content'];
    }
    if ($num == 0) {
        //写入数据库
        $sql1 = "INSERT INTO news(newsID,newsCID,newsAuthor,newsTitle,newsSource,newsSendTime,newsUpdateTime,newsImagePath,newsKeyWords,newsContent) VALUES(null,$cateID,'$newsAuthor','$newsTitle','$newsSource',$newsSendTime,$newsUpdateTime,'$newsImagePath','$newsKeyWords','$newsContent')";
        $query1 = $db->query($sql1);
        $linkNewsID = strval($db->insert_id());
        $sql2 = "UPDATE category SET linkNewsID=$linkNewsID WHERE id=$cateID";
        $query2 = $db->query($sql2);
        if ($query1 == true and $query2 == true) {
            echo "<script>alert('文章添加成功！');window.location.href='listCate.php';</script>";
        } else {
            echo "<script>alert('文章添加失败！');window.location.href='listCate.php';</script>";
        }
    } else {
        //修改数据库中内容
        $sql1 = "SELECT linkNewsID FROM category WHERE id=$cateID";
        $query1 = $db->query($sql1);
        while ($row = $db->fetch_array($query1)) {
            $newsID = $row['linkNewsID'];
        }
        if ($newsID == '') {
            $sql2 = "SELECT * FROM news WHERE newsCID=$cateID LIMIT 1";
            $query2 = $db->query($sql2);
            while ($row = $db->fetch_array($query2)) {
                $newsID = $row['newsID'];
            }
        }
        $sql3 = "UPDATE news SET newsTitle='$newsTitle',newsAuthor='$newsAuthor',newsSource='$newsSource',newsUpdateTime=$newsUpdateTime,newsImagePath='$newsImagePath',newsKeyWords='$newsKeyWords',newsContent='$newsContent' WHERE newsID=$newsID";
        $query3 = $db->query($sql3);
        $linkNewsID = strval($newsID);
        $sql4 = "UPDATE category SET linkNewsId=$linkNewsID WHERE id=$cateID";
        $query4 = $db->query($sql4);
        if ($query3 == true and $query4 == true) {
            echo "<script>alert('文章修改成功！');window.location.href='listCate.php';</script>";
        } else {
            echo "<script>alert('文章修改失败！');window.location.href='listCate.php';</script>";
        }
    }
}
?>
<body>
<div id="right_main">
    <div class="right_content">
        <div class="right_top_menu">
            <h3>
                您当前的位置：后台 > 文章管理 > 单页栏目文章
            </h3>
        </div>
        <div class="right_center_content">
            <form id="form1" method="post" action="">
                <table cellpadding="0" cellspacing="0" border="0" width="100%">
                    <tr>
                        <td valign="top" >
                            <div class="txl_search"></div>
                            <table cellpadding="0" cellspacing="0" border="0" width="100%"  class="sy_table">
                                <tr>
                                    <th colspan="2" >添加文章</th>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">所属栏目：</td>
                                    <td width="90%">
                                        <select name="pid">
                                            <option value="0">请选择文章所属栏目</option>
                                            <?php print_r($gettree); ?>
                                        </select> *不能指定为含有子栏目的栏目！
                                    </td>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">文章标题：</td>
                                    <td width="90%"><input type="text" name="newsTitle"  class="int_text" value="<?php echo $rs['newsTitle']; ?>" size="60"  maxlength="60"/> *必填</td>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">关键字：</td>
                                    <td width="90%"><input type="text" name="newsKeyWords"  class="int_text" value="<?php echo $rs['newsKeyWords']; ?>" size="40"  maxlength="40"/></td>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">文章作者：</td>
                                    <td width="90%"><input type="text" name="newsAuthor"  class="int_text" value="<?php echo $rs['newsAuthor']; ?>" size="40"  maxlength="40"/> *必填</td>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">文章来源：</td>
                                    <td width="90%"><input type="text" name="newsSource"  class="int_text" value="<?php echo $rs['newsSource']; ?>" size="40"  maxlength="40"/></td>
                                </tr>
                                <tr>
                                    <td width="10%" align="right">文章内容：</td>
                                    <td width="90%"><textarea id="editor1" class=".int_area" name="content" style="width:95%;height:400px;"><?php echo stripslashes($rs['newsContent']); ?></textarea></td>
                                </tr>
                                <tr>
                                    <td colspan="2" align="center" height='30'>
                                        <input type="submit" name="addPageNews" class="btn_ipt01" value=" 确定 "/>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </div>
</div>
</body>
</html>
